﻿using System.Collections.Specialized;
using System.Linq;
using System.Windows.Controls;
using Microsoft.Practices.Prism.Regions;
using Telerik.Windows.Controls;
using System.Windows.Data;

namespace Infrastructure.Adapters
{
    public class RadOutlookBarRegionAdapter : RegionAdapterBase<RadOutlookBar>
    {
        public RadOutlookBarRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        { }

        protected override void Adapt(IRegion region, RadOutlookBar regionTarget)
        {
            region.ActiveViews.CollectionChanged += (sender, args) =>
            {
                switch (args.Action)
                {
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                        foreach (UserControl view in args.NewItems)
                        {
                            RadOutlookBarItem item = new RadOutlookBarItem();
                            item.DataContext = view.DataContext;
                            //item.SetBinding(RadOutlookBarItem.HeaderProperty, new Binding());
                            //item.SetBinding(RadOutlookBarItem.TitleProperty, new Binding());
                            item.Style = regionTarget.ItemContainerStyle;
                            item.Content = view;
                            item.FontWeight = System.Windows.FontWeights.Bold;
                            regionTarget.Items.Add(item);
                        }
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                        foreach (UserControl view in args.OldItems)
                        {
                            RadOutlookBarItem viewTab = regionTarget.Items.Cast<RadOutlookBarItem>().Single(o => o.DataContext == view.DataContext);
                            regionTarget.Items.Remove(viewTab);
                        }
                        break;
                }
            };
        }

        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }
    }
}
